package com.powerunion.datacollection.web.struts;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.powerunion.datacollection.service.ProcessInfoManager;
import com.powerunion.datacollection.service.ProcessInstanceManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class ProcessInfoAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(
			ProcessInfoAction.class);
	private ProcessInfoManager pm = null;

	public ProcessInfoAction() {
		super();
		this.pm = new ProcessInfoManager();
	}

	public void finalize() throws Throwable {
		this.pm = null;
		super.finalize();
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String saveActivityInfoStr = request.getParameter("saveActivityInfoStr");
		String saveItemInfoStr = request.getParameter("saveItemInfoStr");
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("流程信息定义:" + saveActivityInfoStr);

			String currentUser = request.getRemoteUser();
			boolean flag = this.pm.save(saveActivityInfoStr, currentUser, saveItemInfoStr);

			if (flag) {
				response.getWriter().write("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}

			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}
	/**
	 * 查找流程定义信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getProcessInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String processId = request.getParameter("processId");
		response.setContentType("text/json;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(this.pm.getProcessInfo(processId));
		logger.debug("processId:"+processId+"||getProcessInfo:"+jsonArray);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	/**
	 * 查找流程实例信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getProcessInstanceInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String processId = request.getParameter("processId");
		response.setContentType("text/json;charset=utf-8");
		ProcessInstanceManager pim = new ProcessInstanceManager();
		JSONArray jsonArray = JSONArray.fromObject(pim.getProcessInfo(processId));
		logger.debug("processId:"+processId+"||getProcessInstanceInfo:"+jsonArray);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	/**
	 * 流程监控
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getProcessSurveillanceInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String processId = request.getParameter("processId");
		response.setContentType("text/json;charset=utf-8");
		ProcessInstanceManager pim = new ProcessInstanceManager();
		JSONArray jsonArray = JSONArray.fromObject(pim.getProcessSurveillanceInfo(processId));
		logger.debug("processId:"+processId+"||getProcessSurveillanceInfo:"+jsonArray);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	
	/**
	 * 任务的流程实例明细 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getTaskDetails(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String processId = request.getParameter("processId");
		response.setContentType("text/json;charset=utf-8");
		ProcessInstanceManager pim = new ProcessInstanceManager();
		JSONArray jsonArray = JSONArray.fromObject(pim.getTaskDetails(processId));
		logger.debug("processId:"+processId+"||getTaskDetails:"+jsonArray);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	/**
	 * 流程定义信息明细
	 * 用于发起任务检测流程信息不全时显示给用户输入不全的信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getProcInfoDetails(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String workflowId = request.getParameter("workflowId");
		String procId = request.getParameter("procId");
		response.setContentType("text/json;charset=utf-8");

		String result = this.pm.getProcessInfoDetails(workflowId,procId);
		logger.debug("procInfoDetails:"+result);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
	public ActionForward validateActivityInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String processId = request.getParameter("procId");
		response.setContentType("text/json;charset=utf-8");

		String validatInfo = this.pm.validateActivityInfo(processId);
		StringBuffer result = new StringBuffer();
		if(StringUtils.isEmpty(validatInfo)){
			result.append("{success:true}");
		}else{
			result.append("{success:false,results:'").append(validatInfo).append("'}");
		}
		
		logger.debug("procInfoDetails:"+result);
		try {
			response.getWriter().print(result);
		} catch (IOException e) {
			logger.error("输出json失败",e);
			return null;
		}
		return null;
	}
	
}
